package club.banyuan;

import java.io.IOException;
import java.net.*;
import java.util.Scanner;

public class UdpSend {
    public static void main(String[] args) throws IOException {
        DatagramSocket datagramSocket = new DatagramSocket();
        Scanner scanner = new Scanner(System.in);
        while (true) {
            InetAddress address;
            int port;
            do {
                System.out.println("请输入目标ip地址：");
                address = InetAddress.getByName(scanner.next());
            } while (!InetAddress.getByName("127.0.0.1").equals(address));

            do {
                System.out.println("请输入端口号：");
                port = scanner.nextInt();
            } while (port != 5000);

            System.out.println("请输入要发送的消息：");
            String msg = scanner.next();
            DatagramPacket datagramPacket = new DatagramPacket(msg.getBytes(), 0, msg.getBytes().length, address, port);
            datagramSocket.send(datagramPacket);
            System.out.println("请选择：1.继续发送 2.结束发送");
            if (scanner.nextInt() == 2) {
                datagramSocket.close();
                break;
            }
        }
    }
}
